﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.COM;
using MINERP.DAL;
namespace MINERP.DAL.PrintManage
{
    public class PhotoDAL : DALBase
    {
        /// <summary>
        /// 得到所有照片的ID通过大版
        /// </summary>
        /// <param name="boardid"></param>
        /// <returns></returns>
        public static List<int> GetPhotoInfoByBorad(int boardId)
        {
            #region old
            //if (b1 != null)
            //{
            //    List<picMakeUp> list  = b1.picMakeUp.ToList();                

            //    Dictionary<int, int> PIList = new Dictionary<int, int>();//<orderproductid,photoid>
            //    foreach (var mu in list)
            //    {
            //        if (!mu.photoInfoReference.IsLoaded)
            //        {
            //            mu.photoInfoReference.Load();
            //        }
            //        if (!mu.photoInfo.orderInfoReference.IsLoaded)
            //        {
            //            mu.photoInfo.orderInfoReference.Load();
            //        }

            //        if (!PIList.Keys.Contains(mu.photoInfo.orderInfo.OrderProductId))
            //        {
            //            PIList.Add(mu.photoInfo.orderInfo.OrderProductId, mu.photoInfo.photoId);
            //        }
            //    }
            //    return PIList.Values.ToList();
            //}
            //return null;
            #endregion

            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                Board b1 = Entities.Board.FirstOrDefault(b => b.BoardId == boardId);
                if (b1 != null)
                {
                    #region new
                    List<int> pidList = new List<int>();

                    if (!b1.picMakeUp.IsLoaded)
                    {
                        b1.picMakeUp.Load();
                    }
                    foreach (var pm in b1.picMakeUp)
                    {
                        if (!pm.photoInfoReference.IsLoaded)
                        {
                            pm.photoInfoReference.Load();
                        }
                        else
                        {
                            pm.photoInfoReference.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                        }

                        pidList.Add(pm.photoInfo.photoId);
                    }
                    //return b1.picMakeUp.Select(pm => pm.photoInfo.photoId).ToList();
                    return pidList;
                    #endregion
                }
                else
                {
                    return null;
                }
            }

        }

        /// <summary>
        /// 查看照片的产品的生产状态

        /// </summary>
        /// <param name="photoid"></param>
        /// <returns>true 代表此张照片所对应的定单（orderinfo）的所有照片全部打印完成，可以进入分片</returns>
        public static bool CheckProductStatus(int photoid)
        {
            //DataEntites.Refresh(System.Data.Objects.RefreshMode.StoreWins, DataEntites.photoInfo);
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                photoInfo pi1 = Entities.photoInfo.Where(pi => pi.photoId == photoid).FirstOrDefault();
                if (pi1 != null)
                {
                    if (!pi1.orderInfoReference.IsLoaded)
                    {
                        pi1.orderInfoReference.Load();
                    }
                    if (!pi1.orderInfo.photoInfo.IsLoaded)
                    {
                        pi1.orderInfo.photoInfo.Load();
                    }
                    else
                    {
                        Entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, pi1.orderInfo.photoInfo);
                        //pi1.orderInfo.photoInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                    }

                    int statusPrinted = (int)PhotoStatus.Printed;
                    int statusFenPian = (int)PhotoStatus.FenPian;

                    //pi1.orderInfo.photoInfo.Select(p=>new {p.photoId,p.status }).ToList();


                    //int temp = pi1.orderInfo.photoInfo.Where(pi => pi.status != statusPrinted && pi.status != statusFenPian).Count();
                    int temp = pi1.orderInfo.photoInfo.Where(pi => pi.status == statusPrinted || pi.status == statusFenPian).Count();
                    //if (pi1.orderInfo.photoInfo.Where(pi => pi.status != status).Count() > 0)
                    //if (temp>0)
                    //{
                    //    return false;
                    //}
                    if (temp == pi1.orderInfo.photoInfo.Count())
                    {
                        return true;
                    }

                }

            }
            return false;
        }


        public static orderInfo GetOrderProductByPhotoInfo(int photoid)
        {
            //using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            //{
            photoInfo pi1 = DataEntites.photoInfo.Where(pi => pi.photoId == photoid).FirstOrDefault();
            if (pi1 != null)
            {
                if (!pi1.orderInfoReference.IsLoaded)
                {
                    pi1.orderInfoReference.Load();
                }
                return pi1.orderInfo;
            }
            //}
            return null;
        }

    }
}
